
// const localCookie = {}
// //获取cookie、
// export function getCookie(name) {
//  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
//  if (arr = document.cookie.match(reg))
//   return (arr[2]);
//  else
//   return null;
// }
 
// //设置cookie,增加到vue实例方便全局调用
// export function setCookie (c_name, value, expiredays) {
//  var exdate = new Date();
//  exdate.setDate(exdate.getDate() + expiredays);
//  document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
// };
 
// //删除cookie
// export function delCookie (name) {
//  var exp = new Date();
//  exp.setTime(exp.getTime() - 1);
//  var cval = getCookie(name);
//  if (cval != null)
//   document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
// };

var localCookies = {
  // 'm.smzdm.com': {}
}

// var commonParams = {
// 	'__ckguid': 'jKvvVJfFx4lH4wtPHCtRT3',
// 	'_zdmA.uid': 'ZDMA.uTfeOGaGV.1545199433.2419200',
// 	'device_id': '169878998615451991162230589fb2ccc5891662eda7ca214400c9b870; Hm_lvt_9b7ac3d38f30fe89ff0b8a0546904e58=1545199116',
// 	'_ga': 'GA1.2.1137280389.1545199118'
// }

var commonParams = {} 

// 获取 cookie数组，格式为 ['a=b', 'c=d'] 返回对象 {a: b, c: d}
function normalCookie(cookieArr = []) {
	var reObj = {}
	for (var i = cookieArr.length - 1; i >= 0; i--) {
		var strItem = cookieArr[i]
		var sFirst = strItem.indexOf('=')
		var key = strItem.substring(0, sFirst)
		var value = strItem.substring(sFirst + 1)
		reObj[key] = value
	}
	return reObj
}

function setCookieObj(host, cookieObj) {
	var oldCookie = getCookieObj(host) || {}
	var newCookie = Object.assign({}, cookieObj, oldCookie)
	localCookies[host] = newCookie
}

function getCookieObj(host) {
	if (localCookies[host]) {
		return Object.assign(commonParams, localCookies[host])
	} else {
		return {}
	}
}

function formateCookieToString(cookie) {
	let reArr = []
	for ( key in cookie) {
		reArr.push('' + key + '=' + cookie[key] )
	}
	return reArr.join('; ')
}

module.exports = {
	normalCookie,
	setCookieObj,
	getCookieObj,
	formateCookieToString
}
